import pandas as pd
import altair as alt
df = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-11-13/malaria_deaths.csv')
#df.head()
df.columns = ['name', 'ISO3166-1-Alpha-3','Year','Death_Rate']
#df['Year'] = pd.to_datetime(df['Year'], format='%Y')
country_info = pd.read_csv("https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv",dtype = 'str')
df_new = df[df['ISO3166-1-Alpha-3'].isin(country_info['ISO3166-1-Alpha-3'])]
df_new.head()
name ISO3166-1-Alpha-3 Year Death_Rate
0 Afghanistan AFG 1990 6.802930
1 Afghanistan AFG 1991 6.973494
2 Afghanistan AFG 1992 6.989882
3 Afghanistan AFG 1993 7.088983
4 Afghanistan AFG 1994 7.392472
df_final = pd.merge(df_new, country_info , on = 'ISO3166-1-Alpha-3', how = 'left')
df_final = df_final[['Year','Death_Rate','ISO3166-1-numeric']]
df_final.head()
Year Death_Rate ISO3166-1-numeric
0 1990 6.802930 004
1 1991 6.973494 004
2 1992 6.989882 004
3 1993 7.088983 004
4 1994 7.392472 004
alt.data_transformers.disable_max_rows()
countries = alt.topo_feature(data.world_110m.url, 'countries')
slider = alt.binding_range(
    step=1,
    min=1990, 
    max=2016
)

select_date = alt.selection_single(
    name="slider", 
    fields=['Year'],
    bind=slider, 
)
from vega_datasets import data
alt.data_transformers.disable_max_rows()
countries = alt.topo_feature(data.world_110m.url, 'countries')
slider = alt.binding_range(
    step=1,
    min=1990, 
    max=2016
)

select_date = alt.selection_single(
    name="slider", 
    fields=['Year'],
    bind=slider, 
)

alt.Chart(df_final).mark_geoshape()\
    .encode(color='Death_Rate:Q')\
    .add_selection(select_date)\
    .transform_filter(select_date)\
    .transform_lookup(
        lookup='ISO3166-1-numeric',
        from_=alt.LookupData(countries, key='id', fields=["type", "properties", "geometry"])
    )\
    .project('orthographic')\
    .properties(
        width=400,
        height=300,
        title='Malaria Death Rate (per 100,000 people) '
    )
df_final['Year'] = pd.to_datetime(df_final['Year'], format='%Y')
country_list = df_final['ISO3166-1-numeric'].dropna().unique()
country_list = country_list.tolist()
dropdown = alt.binding_select(
    options = country_list
)
select_country = alt.selection_single(
    name="dropdown", 
    fields=['ISO3166-1-numeric'],
    bind = dropdown, 
)
alt.Chart(df_final).mark_line().encode(
    x='Year',
    y='Death_Rate',
).add_selection(
   select_country
).transform_filter(select_country)
df1 = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-11-13/malaria_inc.csv')
df1.columns = ['name', 'ISO3166-1-Alpha-3','Year','Incidence_Rate']
df1_new = df1[df1['ISO3166-1-Alpha-3'].isin(country_info['ISO3166-1-Alpha-3'])]
df1_new.head()
name ISO3166-1-Alpha-3 Year Incidence_Rate
0 Afghanistan AFG 2000 107.100000
1 Afghanistan AFG 2005 46.500000
2 Afghanistan AFG 2010 23.900000
3 Afghanistan AFG 2015 23.600000
4 Algeria DZA 2000 0.037746
df1_final = pd.merge(df1_new, country_info , on = 'ISO3166-1-Alpha-3', how = 'left')
df1_final = df1_final[['Year','Incidence_Rate','ISO3166-1-numeric']]
df1_final.head()
Year Incidence_Rate ISO3166-1-numeric
0 2000 107.100000 004
1 2005 46.500000 004
2 2010 23.900000 004
3 2015 23.600000 004
4 2000 0.037746 012
alt.data_transformers.disable_max_rows()
countries = alt.topo_feature(data.world_110m.url, 'countries')
slider1 = alt.binding_range(
    step=5,
    min=2000, 
    max=2015
)

select_date1 = alt.selection_single(
    name="slider", 
    fields=['Year'],
    bind=slider1, 
)
from vega_datasets import data

alt.Chart(df1_final).mark_geoshape()\
    .encode(color='Incidence_Rate:Q')\
    .add_selection(select_date1)\
    .transform_filter(select_date1)\
    .transform_lookup(
        lookup='ISO3166-1-numeric',
        from_=alt.LookupData(countries, key='id', fields=["type", "properties", "geometry"])
    )\
    .project('orthographic')\
    .properties(
        width=400,
        height=300,
        title='Malaria Incidence Rate (per 1,000 people) '
    )
df2 = pd.read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018/2018-11-13/malaria_deaths_age.csv')
df2.columns = ['index','name', 'ISO3166-1-Alpha-3','Year','Age_Group','Death_Rate']
df2_new = df2[df2['ISO3166-1-Alpha-3'].isin(country_info['ISO3166-1-Alpha-3'])]
df2_new.head()
index name ISO3166-1-Alpha-3 Year Age_Group Death_Rate
0 1 Afghanistan AFG 1990 Under 5 184.606435
1 2 Afghanistan AFG 1991 Under 5 191.658193
2 3 Afghanistan AFG 1992 Under 5 197.140197
3 4 Afghanistan AFG 1993 Under 5 207.357753
4 5 Afghanistan AFG 1994 Under 5 226.209363
df2_final = pd.merge(df2_new, country_info , on = 'ISO3166-1-Alpha-3', how = 'left')
df2_final = df2_final[['Year','Death_Rate','ISO3166-1-numeric','Age_Group']]
df2_final.head()
Year Death_Rate ISO3166-1-numeric Age_Group
0 1990 184.606435 004 Under 5
1 1991 191.658193 004 Under 5
2 1992 197.140197 004 Under 5
3 1993 207.357753 004 Under 5
4 1994 226.209363 004 Under 5
df2_final['Year'] = pd.to_datetime(df2_final['Year'], format='%Y')
country_list2 = df2_final['ISO3166-1-numeric'].dropna().unique()
country_list2 = country_list2.tolist()
dropdown2 = alt.binding_select(
    options = country_list2
)
select_country2 = alt.selection_single(
    name="dropdown", 
    fields=['ISO3166-1-numeric'],
    bind = dropdown2, 
)
alt.Chart(df2_final).mark_point().encode(
    x='Year',
    y='Death_Rate',
    color = 'Age_Group'
).add_selection(
   select_country2
).transform_filter(select_country2)